package com.shanling.shanlingcontroller.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.chipsguide.lib.bluetooth.entities.BluetoothDeviceMusicSongEntity;
import com.chipsguide.lib.bluetooth.managers.BluetoothDeviceManager;
import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.adapter.CommonAdapter;
import com.shanling.shanlingcontroller.adapter.ViewHolder;
import com.shanling.shanlingcontroller.base.BaseAppCompatActivity;
import com.shanling.shanlingcontroller.bean.Constants;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.bean.Music;
import com.shanling.shanlingcontroller.manager.DeviceMusicManager;
import com.shanling.shanlingcontroller.music.player.PlayManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xiaobai on 2019/12/12.
 */

public class DeviceMusicActivity extends BaseAppCompatActivity implements DeviceMusicManager.onDeviceModeChangedListener, AdapterView.OnItemClickListener {

    @BindView(R.id.lv_songs)
    ListView mLvSongs;

    private CommonAdapter<BluetoothDeviceMusicSongEntity> mSongAdapter;

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_device_music;
    }

    @OnClick(R.id.iv_back)
    public void onBackClick(){
        finish();
    }

    @Override
    protected void initViewsAndEvents() {
        DeviceMusicManager.getInstance().addOnDeviceMusicSongChangedListener(deviceMusicSongChangedListener);
        DeviceMusicManager.getInstance().setOnDeviceModeChangedListener(this);
    }

    @Override
    protected void initData() {
        mSongAdapter = new CommonAdapter<BluetoothDeviceMusicSongEntity>(this, getDeviceMusicSongEntities(), R.layout.item_song) {
            @Override
            public void convert(ViewHolder helper, int position, BluetoothDeviceMusicSongEntity item) {
                TextView tv_songName = helper.getView(R.id.tv_song_name);
                TextView tv_albumName = helper.getView(R.id.tv_ablumname);
                ImageView iv_action = helper.getView(R.id.iv_action);
                iv_action.setVisibility(View.INVISIBLE);
                tv_songName.setText(item.getName());
                tv_albumName.setText(getResources().getString(R.string.unknown));
                BluetoothDeviceMusicSongEntity entity = DeviceMusicManager.getInstance().getCurrentSong();
                if (item.getName().equals(entity.getName())){
                    tv_songName.setTextColor(ContextCompat.getColor(mContext, R.color.text_color));
                    tv_albumName.setTextColor(ContextCompat.getColor(mContext, R.color.text_color));
                }else {
                    tv_songName.setTextColor(ContextCompat.getColor(mContext, R.color.text_normal));
                    tv_albumName.setTextColor(ContextCompat.getColor(mContext, R.color.text_hint_color));
                }
            }
        };
        mLvSongs.setAdapter(mSongAdapter);
        mLvSongs.setOnItemClickListener(this);
    }

    private List<BluetoothDeviceMusicSongEntity> getDeviceMusicSongEntities(){
        return DeviceMusicManager.getInstance().getDeviceMusicSongEntities();
    }

    private DeviceMusicManager.OnDeviceMusicSongChangedListener deviceMusicSongChangedListener = new DeviceMusicManager.OnDeviceMusicSongChangedListener() {
        @Override
        public void onDeviceMusicSongListChanged() {
            mSongAdapter.refresh(getDeviceMusicSongEntities());
        }

        @Override
        public void onDeviceMusicLoopModeChanged(int mode) {

        }

        @Override
        public void onDeviceMusicPlayStateChanged(int playState) {

        }

        @Override
        public void onDeviceMusicSongChanged(BluetoothDeviceMusicSongEntity bluetoothDeviceMusicSongEntity) {
            mSongAdapter.refresh(getDeviceMusicSongEntities());
        }

        @Override
        public void onDeviceMusicProgressUpdate(int currentPosition, int duration) {

        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DeviceMusicManager.getInstance().removeOnDeviceMusicSongChangedListener(deviceMusicSongChangedListener);
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected void onEventComing(EventCenter eventCenter) {

    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    public void onDeviceModeChanged(int mode) {
        if (mode != BluetoothDeviceManager.Mode.CARD){
            finish();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        BluetoothDeviceMusicSongEntity entity = getDeviceMusicSongEntities().get(position);
        DeviceMusicManager.getInstance().selectPlay(entity.getIndex());
        DeviceMusicPlayActivity.launch(this, entity);
    }
}
